#include <time.h>
#include "MCE.h"
#include "MCE_Mode.h"


/*
Define characters
*/
#define NUM_MODES 21
struct CHARMAP {
       int tag;
       char ln[8];
       } charmap [NUM_MODES] = 
         { TAG_Play, 
                                           B10000,
                                           B11000,
                                           B11100,
                                           B11110,
                                           B11100,
                                           B11000,
                                           B10000,
                                           B00000,
           TAG_Stop, 
                                           B11111,
                                           B11111,
                                           B11111,
                                           B11111,
                                           B11111,
                                           B11111,
                                           B11111,
                                           B00000,
           TAG_Pause, 
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B00000,
           TAG_Next,
                                           B00000,
                                           B10100,
                                           B10110,
                                           B10111,
                                           B10111,
                                           B10110,
                                           B10100,
                                           B00000,
           TAG_Prev,
                                           B00000,
                                           B00101,
                                           B01101,
                                           B11101,
                                           B11101,
                                           B01101,
                                           B00101,
                                           B00000,
           TAG_Recording,
                                           B00000,
                                           B00100,
                                           B01110,
                                           B11111,
                                           B11111,
                                           B01110,
                                           B00100,
                                           B00000,
           TAG_FF1,
                                           B00000,
                                           B10100,
                                           B11010,
                                           B11101,
                                           B11101,
                                           B11010,
                                           B10100,
                                           B00000,
           TAG_FF2,
                                           B00000,
                                           B10100,
                                           B11010,
                                           B11101,
                                           B11101,
                                           B11010,
                                           B10100,
                                           B00000,
           TAG_FF3,
                                           B00000,
                                           B10100,
                                           B11010,
                                           B11101,
                                           B11101,
                                           B11010,
                                           B10100,
                                           B00000,
           TAG_Rewind1,
                                           B00000,
                                           B00101,
                                           B01011,
                                           B10111,
                                           B10111,
                                           B01011,
                                           B00101,
                                           B00000,
           TAG_Rewind2,
                                           B00000,
                                           B00101,
                                           B01011,
                                           B10111,
                                           B10111,
                                           B01011,
                                           B00101,
                                           B00000,
           TAG_Rewind3,
                                           B00000,
                                           B00101,
                                           B01011,
                                           B10111,
                                           B10111,
                                           B01011,
                                           B00101,
                                           B00000,
           TAG_NextFrame,
                                           B00000,
                                           B10100,
                                           B10110,
                                           B10111,
                                           B10111,
                                           B10110,
                                           B10100,
                                           B00000,
           TAG_PrevFrame,
                                           B00000,
                                           B00101,
                                           B01101,
                                           B11101,
                                           B11101,
                                           B01101,
                                           B00101,
                                           B00000,
           TAG_SlowMotion1,
                                           B01011,
                                           B01011,
                                           B01011,
                                           B11011,
                                           B01011,
                                           B01011,
                                           B01011,
                                           B00000,
           TAG_SlowMotion2,
                                           B01011,
                                           B01011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B01011,
                                           B01011,
                                           B00000,
           TAG_SlowMotion3,
                                           B01011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B11011,
                                           B01011,
                                           B00000,
           TAG_Mute,
                                           B01000,
                                           B01101,
                                           B01010,
                                           B01111,
                                           B01011,
                                           B11010,
                                           B01100,
                                           B01000,
           TAG_PhoneCall,
                                           B00000,
                                           B11111,
                                           B10101,
                                           B00100,
                                           B00100,
                                           B00100,
                                           B00100,
                                           B00000,
           TAG_CCMuteOff,
                                           B01000,
                                           B01100,
                                           B01010,
                                           B01011,
                                           B01011,
                                           B01010,
                                           B01100,
                                           B01000,
           TAG_Volume,
                                           B01000,
                                           B01100,
                                           B01010,
                                           B01011,
                                           B01011,
                                           B01010,
                                           B01100,
                                           B01000
         };  
                                 

static double start_time,end_time;         // for the display mode counter
static int Prev_TAG_Mode = TAG_Stop;

//================================================================================================================================
// Function to return characters for the status of the current mode
//================================================================================================================================
extern char * __stdcall get_mode_status(int mode)
{  int i,c,d,found,myvol;
   static char modeout[128];
   static char map[8];
   char str[128];

   strcpy(modeout," ");                     // if not one of the modes then you get a blank.
   if ((mode==TAG_Volume) && (getbi(TAG_UseVolIcon)==0)) // Do something special for volume
     { myvol=((geti(TAG_Volume)+4)*8)/50;
       if (myvol>8) myvol=8; if (myvol<0) myvol=0;
       for (i=0;i<8;i++) { if (myvol>i) map[i]=31; else map[i]=0; }
       c=geti(TAG_ModeChar); if (c==1) { d=176; } else if (c==2) { d=158; } else { d=c+128; }
       sprintf(modeout,"$CustomChar(%d,%d,%d,%d,%d,%d,%d,%d,%d)$Chr(%d)",c,
                       map[7],map[6],map[5],map[4],map[3],map[2],map[1],map[0],d); 
     }
   else if ((mode==TAG_Mute) && (getbi(TAG_Mute)==0)) // Do something special for mute off
     { sprintf(modeout,"%s",getst(TAG_CCMuteOff)); }
   else    // find the mode in the character map table
     { found=0;
       for (i=0;i<NUM_MODES;i++){ if (charmap[i].tag==mode) { found=1; break; } }
       if (found==1) { sprintf(modeout,"%s",getst(TAG_CustomChar1-i)); }
      }
   Tlog("get_mode_status",modeout);
   return modeout;
}


     
//================================================================================================================================
// Function to copy the custom char definitions to the tag table
//================================================================================================================================
extern void __stdcall init_modes(void)
{ int i,c,d;
  char str[256];

   c=geti(TAG_ModeChar); if (c==1) { d=176; } else if (c==2) { d=158; } else { d=c+128; }
  
  for (i=0; i<NUM_MODES; i++)
    { sprintf(str,"$CustomChar(%d,%d,%d,%d,%d,%d,%d,%d,%d)$Chr(%d)",c,
                                            charmap[i].ln[0],charmap[i].ln[1],charmap[i].ln[2],charmap[i].ln[3],
                                            charmap[i].ln[4],charmap[i].ln[5],charmap[i].ln[6],charmap[i].ln[7],d);
      putst(TAG_CustomChar1-i,str);
    }
}    


//================================================================================================================================
// routine to update the play status tags and set the timer
//================================================================================================================================
extern void   __stdcall set_mode(int tagval,int play, int stop, int pause, int ff1, int ff2, int ff3, 
                                   int rw1, int rw2, int rw3, int sm1, int sm2, int sm3)
{ putbi(TAG_Play,play);       putbi(TAG_Stop,stop);       putbi(TAG_Pause,pause); 
  putbi(TAG_FF1,ff1);         putbi(TAG_FF2,ff2);         putbi(TAG_FF3,ff3);
  putbi(TAG_Rewind1,rw1);     putbi(TAG_Rewind2,rw2);     putbi(TAG_Rewind3,rw3); 
  putbi(TAG_SlowMotion1,sm1); putbi(TAG_SlowMotion2,sm2); putbi(TAG_SlowMotion3,sm3); 
  Prev_TAG_Mode=tagval;
  start_counter(tagval);                          // Initialise the counter
  putst(TAG_Mode,getdisplay(tagval));

}

//================================================================================================================================
// starts the counter running n.b. does not update the string in the mode status
//================================================================================================================================
extern void   __stdcall start_counter(int tag)
{ 
  puti(TAG_StatusCounter,geti(TAG_StatusCount));   // Initialise the counter
  puti(TAG_Mode,tag); puti(TAG_DisplayMode,tag);     
  putst(TAG_DisplayMode,getdisplay(tag));            // 
  start_time=(double)clock()/CLOCKS_PER_SEC;       // Counter start time
  putst(TAG_Icon,get_mode_status(tag));
}

//================================================================================================================================
// checks the counter running n.b. does not update the string in the mode status
//================================================================================================================================
extern void __stdcall MCE_check_counter()
{ double tdiff; int counter;

  if (geti(TAG_StatusCounter)>0)
    {
      end_time=(double)clock()/CLOCKS_PER_SEC;       // Counter time
      tdiff=end_time-start_time;                     // in seconds
      counter=geti(TAG_StatusCount)-(int)tdiff;
      if (counter<1) 
        { counter=0; puti(TAG_StatusCounter,counter);
          puti(TAG_Mode,Prev_TAG_Mode); puti(TAG_DisplayMode,Prev_TAG_Mode);     
          putst(TAG_DisplayMode,getdisplay(Prev_TAG_Mode));            // 
          putst(TAG_Icon,get_mode_status(Prev_TAG_Mode));
        }
    }
    MCE_check_for_reinit();
}
